package cn.zhaozhiqi.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.zhaozhiqi.exception.BussinessException;
import cn.zhaozhiqi.user.domain.User;
import cn.zhaozhiqi.user.mapping.UserMapper;
import cn.zhaozhiqi.user.query.UserQuery;
import cn.zhaozhiqi.user.service.IUserService;
import cn.zhaozhiqi.util.PageList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getByID(Long id) {
        return userMapper.getByID(id);
    }

    @Override
    public void getAll() {
        userMapper.getAll();
    }

    @Override
    public void addData(User user) {
        //在新增用户的时候我们需要判断用户名是否已经被注册过，可使用hutool工具包内的包装类进行
        User tmp = userMapper.getDataUser(user.getUserName());
        if (ObjectUtil.isNotNull(tmp)){
            throw new BussinessException("用户名已经被注册！！");
        }
        userMapper.addData(user);
    }

    @Override
    public void updateByID(User user) {
         userMapper.updateByID(user);
    }

    @Override
    public void deleteByID(Long id) {
        userMapper.deleteByID(id);
    }

    @Override
    public PageInfo<User> userLimit(UserQuery query) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<User> list = userMapper.getSearchList(query);
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        return userPageInfo;
    }

    @Override
    public void batchdelete(List<Long> ids) {
        userMapper.batchdelete(ids);
    }
}
